import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/InputView.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JFlatButton.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JPCH.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/utils/configurationFile/ConfigurationFile.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';

import 'ChangeDefaultPWDViewModel.dart';

class ChangeDefaultPWD extends BaseStatefulWidget {
  @override
  State<StatefulWidget> initState() {
    return ChangeDefaultPWDSate();
  }
}

class ChangeDefaultPWDSate
    extends BaseState<ChangeDefaultPWDViewModel, ChangeDefaultPWD> {
  TextEditingController _newPWDCtr = TextEditingController();

  ///新密码
  TextEditingController _confirmPWDCtr = TextEditingController();

  ///确认新密码

  ///新密码错误 信息
  String _errorText;
  bool _obscureNewPWD = true;

  ///是否隐藏
  ///确认新密码错误 信息
  String _confirmErrorText;
  bool _obscureConfirmPWD = true;

  ///是否隐藏

  @override
  Widget initWidget() {
    return Scaffold(
      appBar: MTopBar(title: "修改密码").create(),
      body: _bodVew(),
    );
  }

  _bodVew() {
    return Column(
      children: <Widget>[
        ///新密码
        InputView.outlineInputBorder(
          padding: EdgeInsets.fromLTRB(JScreenSpace, JSpace, JScreenSpace, 0),
          labelText: "请输入新密码",
          labelStyle: TextStyle(fontSize: Dimens.textSize28),
          textEditingController: _newPWDCtr,
          errorText: _errorText,
          obscureText: _obscureNewPWD,
          onChanged: ((value) {
            String errorText;

            if (6 > value.length) {
              errorText = "密码不能少于6位";
            } else if (value.length > 12) {
              errorText = "密码不能大于12位";
            }

            setState(() {
              _errorText = errorText;
            });
          }),
          suffixIcon: IconButton(
              icon: _obscureNewPWD == true
                  ? Icon(Icons.visibility_off)
                  : Icon(Icons.visibility),
              onPressed: () {
                setState(() {
                  _obscureNewPWD = !_obscureNewPWD;
                });
              }),
        ),

        ///确认新密码
        InputView.outlineInputBorder(
          padding: EdgeInsets.fromLTRB(JScreenSpace, JSpace, JScreenSpace, 0),
          labelText: "重复输入新密码",
          labelStyle: TextStyle(fontSize: Dimens.textSize28),
          textEditingController: _confirmPWDCtr,
          errorText: _confirmErrorText,
          obscureText: _obscureConfirmPWD,
          onChanged: ((value) {
            String confirmErrorText;

//            if (6 > value.length) {
//              confirmErrorText =  "密码不能少于6位";
//            } else if (value.length > 12) {
//              confirmErrorText = "密码不能大于12位";
//            }

            if (_newPWDCtr.text != value) {
              confirmErrorText = "两次输入的新密码不一致";
            }

            setState(() {
              _confirmErrorText = confirmErrorText;
            });
          }),
          suffixIcon: IconButton(
              icon: _obscureConfirmPWD == true
                  ? Icon(Icons.visibility_off)
                  : Icon(Icons.visibility),
              onPressed: () {
                setState(() {
                  _obscureConfirmPWD = !_obscureConfirmPWD;
                });
              }),
        ),
        SizedBox(
          height: 50,
        ),

        ///确认修改
        JFlatButton(
          title: "确认修改",
          color: JPch.primaryColor(context),
          frameMargin: EdgeInsets.all(15),
          onPressed: () => _confirmAction(),
        )
      ],
    );
  }

  ///判断是否输入
  _judgeEmpty() {
    if (_newPWDCtr.text == "") {
      ToastUtils.showToast("请输入新密码");
      return true;
    }
    if (_confirmPWDCtr.text == "") {
      ToastUtils.showToast("请重复输入新密码");
      return true;
    } else {
      if (_newPWDCtr.text != _confirmPWDCtr.text) {
        ToastUtils.showToast("两次输入的新密码不一致");
        return true;
      }
      if (6 > _newPWDCtr.text.length) {
        ToastUtils.showToast("密码不能少于6位");
        return true;
      } else if (_newPWDCtr.text.length > 12) {
        ToastUtils.showToast("密码不能大于12位");
        return true;
      }
    }

    return false;
  }

  ///确认按钮
  _confirmAction() {
    if (_judgeEmpty()) {
      return;
    }
    String newPassword = _confirmPWDCtr.text;
    viewModel.changePwd(newPassword);
    viewModel.dataStream().listen((baseData) {
      if (baseData != null && baseData.code == 0) {
        ToastUtils.showToast(baseData.msg);
        Navigator.pop(context);
      } else if (baseData != null && baseData.code == 1) {
        ToastUtils.showToast(baseData.msg);
      } else {
        ToastUtils.showToast("修改失败");
      }
    });
  }
}
